﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace WeatherWS
{
    public class City
    {
        #region properties

        public string LocCode { get; set; }
        public string Name { get; set; }

        #endregion

        #region contructors

        public City()
            : this(string.Empty, string.Empty)
        { }

        public City(string code, string name)
        {
            this.LocCode = code;
            this.Name = name;
        }

        #endregion

        #region methods

        /// <summary>
        /// Gets all cities from xml file
        /// </summary>
        /// <param name="city"></param>
        public static List<City> GetCities(string filename)
        {
            return LoadCities(filename);
        }

        private static List<City> LoadCities(string filename)
        {
            // Gets data
            XDocument xmlDoc = XDocument.Load(filename);
            return (from discendant in xmlDoc.Descendants("City")
                        select new City
                        {
                            // Get attribute of element 'City'
                            LocCode = discendant.Attribute("locCode").Value,
                            // Get innerText/content of element 'City'
                            Name = discendant.Value
                        }).ToList<City>();
            //try
            //{
            //    // Convert var type to list type
            //    return feeds.ToList<City>();
            //}
            //catch
            //{
            //    // Error reading xml
            //    return new List<City>();
            //}
        }

        #endregion

    }
}